Skip to content

feat(tracemetrics): Use reference map to render equations#112817

Merged
narsaynorath merged 13 commits intomasterfrom
nar/feat/tracemetrics-use-references-as-intermediate-representation
Apr 14, 2026
Merged

feat(tracemetrics): Use reference map to render equations#112817
narsaynorath merged 13 commits intomasterfrom
nar/feat/tracemetrics-use-references-as-intermediate-representation

Conversation

@narsaynorath
Copy link
Copy Markdown
Member

Updates the references hook to return a reference map where the keys are the query label (A, B, etc) and the values are the aggregate which represent that query for use in equations. If there's a filter, we convert it to the _if format. e.g. a count on metric A for geo.country:Canada would look like

count_if(`geo.country:Canada`,value,metricA,distribution,none)

The equation builder will use the reference map to turn the input expression into the A, B, etc form and render that form. Any updates to that will call a callback that "resolves" the equation to its original aggregate form so we can use it to make downstream events-timeseries request.

I added an effect to pick up on when the reference map changes, because if I'm plotting A + B and B changes, I'd want to call the callback so we continue to plot the correct data.

@narsaynorath narsaynorath requested a review from a team as a code owner April 13, 2026 17:12
@github-actions github-actions bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Apr 13, 2026
Accidentally deleted some conditional code
)
.map((metricQuery, index) => metricQuery.label ?? getVisualizeLabel(index, false))
.map((metricQuery, index) => [
metricQuery.label ?? getVisualizeLabel(index, false),
Copy link
Copy Markdown
Member

@JoshuaKGoldberg JoshuaKGoldberg Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where the keys are the query label (A, B, etc)

Just noting, similar to #112893 (comment), that IMO it'd be good to eventually have these use some form of unique ID. I've been in a couple meetings lately where it was noted that using the order-dependent labels causes surprising behavior on reordering.

I think this PR is a step in the right direction. 🚀

Copy link
Copy Markdown
Member Author

@narsaynorath narsaynorath Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in a previous PR I annotate the metric queries with a slightly more stable ID! It gets auto-generated on load so it's not persisted and resets on refresh (which I wanted to, so I could "compact" the labels by iterating them again from the beginning) and Nick was able to use them for his reordering work. I hope we're okay for now 🙏
#112675

Copy link
Copy Markdown
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool!! Good luck with the merge conflicts though 🥲

@sentry
Copy link
Copy Markdown
Contributor

sentry bot commented Apr 14, 2026

Sentry Snapshot Testing

Name Added Removed Modified Renamed Unchanged Status
sentry-frontend
sentry-frontend
0 0 0 0 204 ✅ Unchanged

Copy link
Copy Markdown
Contributor

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d9f35c5. Configure here.

const visualize = metricConfig.visualizes[0]!;
const parsed = parseFunction(visualize.yAxis);
if (parsed) {
return `${parsed.name}_if(\`${metricConfig.query}\`,${parsed.arguments.join(',')})`;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Queries with spaces break _if format tokenization

Medium Severity

resolveMetricReference generates _if format strings like count_if(`status:ok browser:Chrome`,value,...) where the query is backtick-wrapped. When the query contains spaces (common for multi-condition filters), unresolveExpression fails because the PEG grammar's name rule ([^()\t\n, "]+) stops at spaces, preventing the entire _if(...) call from being parsed as a single TokenFunction token. No single token's text will match the reversed reference map key, so the equation builder displays raw function calls instead of reference labels like A or B.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit d9f35c5. Configure here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is okay for now. I've worked around it and I do not want to update the grammar yet. I can keep this in mind to follow up with because I had to remove validity checks against the resolved equation to make this work.

@narsaynorath narsaynorath merged commit dd7fb18 into master Apr 14, 2026
64 checks passed
@narsaynorath narsaynorath deleted the nar/feat/tracemetrics-use-references-as-intermediate-representation branch April 14, 2026 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants